<template>
  <PageWrapper title="角色管理" content="角色管理，对角色的增删改查操作">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增角色</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column['key'] === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'ant-design:form-outlined',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:node-index-outlined',
                onClick: handlePermissionEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
    <RolePermissionModal @register="registerModal" @success="handlePermissionSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { queryRolePage, removeRoleById } from '/@/api/system/system';
  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './RoleDrawer.vue';
  import { columns, searchFormSchema } from './role.data';
  import { RoleReq } from '/@/api/system/modal/roleModel';
  import { PageWrapper } from '/@/components/Page';
  import { useModal } from '/@/components/Modal';
  import RolePermissionModal from '/@/views/system/role/RolePermissionModal.vue';

  export default defineComponent({
    name: 'RoleManagement',
    components: {
      RolePermissionModal,
      BasicTable,
      RoleDrawer,
      TableAction,
      PageWrapper,
    },
    setup() {
      const [registerModal, { openModal }] = useModal();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '角色列表',
        api: queryRolePage,
        fetchSetting: {
          pageField: 'current',
          sizeField: 'size',
          totalField: 'total',
          listField: 'records',
        },
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          showAdvancedButton: true,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handlePermissionEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      function handlePermissionSuccess() {
        reload();
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleSuccess() {
        reload();
      }

      async function handleDelete(record: Recordable) {
        const deptRole: RoleReq = {
          id: record?.id,
        };
        await removeRoleById(deptRole);
        await reload();
      }

      return {
        registerTable,
        registerDrawer,
        registerModal,
        handleCreate,
        handlePermissionEdit,
        handlePermissionSuccess,
        handleEdit,
        handleSuccess,
        handleDelete,
      };
    },
  });
</script>
